package top.sdwu_top.kbase.common.handler;


import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import top.sdwu_top.kbase.entity.KbaseAuthUser;

import java.util.Date;

@Slf4j
public class MybatisPlusMetaObjectHandler implements MetaObjectHandler {

    private static final int DEL_FLAG_DEFAULT = 0;

    private static final String CREATE_BY = "createdBy";
    private static final String CREATE_TIME = "createdTime";
    private static final String UPDATE_BY = "updatedBy";
    private static final String UPDATE_TIME = "updatedTime";
    private static final String DEL_FLAG = "delFlag";

    private KbaseAuthUser user = new KbaseAuthUser();

    @Override
    public void insertFill(MetaObject metaObject) {
        log.debug("*************************");
        log.debug("insert fill");
        log.debug("*************************");
        Authentication authentication = SecurityContextHolder.getContext()
                .getAuthentication();
        if (null != authentication && null != authentication.getPrincipal()) {
            user = (KbaseAuthUser) authentication.getPrincipal();
        }
        if (null == user || null == user.getId()) {
            user.setId("暂无");
        }
        if (isFill(CREATE_BY, metaObject)) {
            this.setInsertFieldValByName(CREATE_BY,user.getId(), metaObject);
        }

        if (isFill(CREATE_TIME, metaObject)) {
            this.setInsertFieldValByName(CREATE_TIME, new Date(), metaObject);
        }

        if (isFill(UPDATE_BY, metaObject)) {
            this.setInsertFieldValByName(UPDATE_BY, user.getId(), metaObject);
        }

        if (isFill(UPDATE_TIME, metaObject)) {
            this.setInsertFieldValByName(UPDATE_TIME, new Date(), metaObject);
        }

        if (isFill(DEL_FLAG, metaObject)) {
            this.setInsertFieldValByName(DEL_FLAG, DEL_FLAG_DEFAULT, metaObject);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        //更新填充
        log.debug("*************************");
        log.debug("updat e fill");
        log.debug("*************************");

        Authentication authentication = SecurityContextHolder.getContext()
                .getAuthentication();
        if (null != authentication && null != authentication.getPrincipal()) {
            user = (KbaseAuthUser) authentication.getPrincipal();
        }
        if (null == user || null == user.getId()) {
            user.setId("暂无");
        }

        if (isFill(UPDATE_BY, metaObject)) {
            this.setUpdateFieldValByName(UPDATE_BY, user.getId(), metaObject);
        }

        if (isFill(UPDATE_TIME, metaObject)) {
            this.setUpdateFieldValByName(UPDATE_TIME, new Date(), metaObject);
        }
    }

    private boolean isFill(String filed, MetaObject metaObject) {
        return metaObject.hasSetter(filed) && getFieldValByName(filed, metaObject) == null;
    }


} 